﻿using System;
using System.Runtime.InteropServices;

namespace KD.Sms.Controls
{
    public class NET_SDK
    {
        public struct NET_DVR_IPADDR
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string sIpV4;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes;

            public void Init()
            {
                byRes = new byte[128];
            }
        }

        public struct NET_DVR_IPDEVINFO_V31
        {
            public byte byEnable;

            public byte byProType;

            public byte byEnableQuickAdd;

            public byte byRes1;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string sUserName;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string sPassword;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string byDomain;

            public NET_DVR_IPADDR struIP;

            public ushort wDVRPort;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 34, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes2;
        }

        public struct NET_DVR_IPCHANINFO
        {
            public byte byEnable;

            public byte byIPID;

            public byte byChannel;

            public byte byIPIDHigh;

            public byte byTransProtocol;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 31, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes;

            public void Init()
            {
                byRes = new byte[31];
            }
        }

        public struct NET_DVR_IPCHANINFO_V40
        {
            public byte byEnable;

            public byte byRes1;

            public ushort wIPID;

            public uint dwChannel;

            public byte byTransProtocol;

            public byte byTransMode;

            public byte byFactoryType;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 241, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes;
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct NET_DVR_GET_STREAM_UNION
        {
            [FieldOffset(0)]
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 492, ArraySubType = UnmanagedType.I1)]
            public byte[] byUnion;

            public void Init()
            {
                byUnion = new byte[492];
            }
        }

        public struct NET_DVR_STREAM_MODE
        {
            public byte byGetStreamType;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes;

            public NET_DVR_GET_STREAM_UNION uGetStream;

            public void Init()
            {
                byGetStreamType = 0;
                byRes = new byte[3];
            }
        }

        public struct NET_DVR_IPPARACFG_V40
        {
            public uint dwSize;

            public uint dwGroupNum;

            public uint dwAChanNum;

            public uint dwDChanNum;

            public uint dwStartDChan;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64, ArraySubType = UnmanagedType.I1)]
            public byte[] byAnalogChanEnable;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64, ArraySubType = UnmanagedType.Struct)]
            public NET_DVR_IPDEVINFO_V31[] struIPDevInfo;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64, ArraySubType = UnmanagedType.Struct)]
            public NET_DVR_STREAM_MODE[] struStreamMode;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes2;
        }

        public struct NET_DVR_JPEGPARA
        {
            public ushort wPicSize;

            public ushort wPicQuality;
        }

        public struct NET_DVR_DEVICEINFO_V30
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 48, ArraySubType = UnmanagedType.I1)]
            public byte[] sSerialNumber;

            public byte byAlarmInPortNum;

            public byte byAlarmOutPortNum;

            public byte byDiskNum;

            public byte byDVRType;

            public byte byChanNum;

            public byte byStartChan;

            public byte byAudioChanNum;

            public byte byIPChanNum;

            public byte byZeroChanNum;

            public byte byMainProto;

            public byte bySubProto;

            public byte bySupport;

            public byte bySupport1;

            public byte bySupport2;

            public ushort wDevType;

            public byte bySupport3;

            public byte byMultiStreamProto;

            public byte byStartDChan;

            public byte byStartDTalkChan;

            public byte byHighDChanNum;

            public byte bySupport4;

            public byte byLanguageType;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes2;
        }

        public struct NET_DVR_ALARMER
        {
            public byte byUserIDValid;

            public byte bySerialValid;

            public byte byVersionValid;

            public byte byDeviceNameValid;

            public byte byMacAddrValid;

            public byte byLinkPortValid;

            public byte byDeviceIPValid;

            public byte bySocketIPValid;

            public int lUserID;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 48, ArraySubType = UnmanagedType.I1)]
            public byte[] sSerialNumber;

            public uint dwDeviceVersion;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string sDeviceName;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I1)]
            public byte[] byMacAddr;

            public ushort wLinkPort;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string sDeviceIP;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string sSocketIP;

            public byte byIpProtocol;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes2;
        }

        public struct NET_DVR_PREVIEWINFO
        {
            public int lChannel;

            public uint dwStreamType;

            public uint dwLinkMode;

            public IntPtr hPlayWnd;

            public bool bBlocked;

            public bool bPassbackRecord;

            public byte byPreviewMode;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
            public byte[] byStreamID;

            public byte byProtoType;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes1;

            public uint dwDisplayBufNum;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 216, ArraySubType = UnmanagedType.I1)]
            public byte[] byRes;
        }

        public delegate void DRAWFUN(int lRealHandle, IntPtr hDc, uint dwUser);
        public delegate void REALDATACALLBACK(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Init();
        [DllImport("HCNetSDK.dll")]
        public static extern uint NET_DVR_GetLastError();

        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_RealPlay_V40(int iUserID, ref NET_DVR_PREVIEWINFO lpPreviewInfo, REALDATACALLBACK fRealDataCallBack_V30, IntPtr pUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_RigisterDrawFun(int lRealHandle, DRAWFUN fDrawFun, uint dwUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CapturePicture(int lRealHandle, string sPicFileName);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CaptureJPEGPicture_NEW(int lUserID, int lChannel, ref NET_DVR_JPEGPARA lpJpegPara, byte[] sJpegPicBuffer, uint dwPicSize, ref uint lpSizeReturned);

        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_Login_V30(string sDVRIP, int wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
    }
}
